﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using System.Data;
using System.Data.Objects.SqlClient;
namespace BLL
{
   public partial class goodsInfoManager
    {
       //多条数据添加
       public void addMore( goodsInfo t)
       {
           using (ZG_OAEntities zg = new ZG_OAEntities())  
           {
               zg.goodsInfo.MergeOption = System.Data.Objects.MergeOption.NoTracking;
               zg.goodsInfo.AddObject(t);
               zg.SaveChanges();
               zg.Detach(t);
           }
          
           
       }

       public List<goodsInfo> GetAll(int wasteBookID)
       {
           return db.GetAll().Where(p => p.wasteBookId == wasteBookID).ToList<goodsInfo>();
       }

       //根据条件获取折扣
       public IQueryable<goodsInfo> GetByTime(string startTime, string endTime, string startNum,string endNum)
       {
           ZG_OAEntities ds = new ZG_OAEntities();
            
               var query = from obj in ds.goodsInfo orderby obj.id descending select obj;
               if (!string.IsNullOrEmpty(startTime) && startTime != "null")
               {
                   DateTime dt = Convert.ToDateTime(startTime);
                   query = from obj in query where obj.selltime >= dt orderby obj.id descending select obj;
               }
               if (!string.IsNullOrEmpty(endTime) && endTime != "null")
               {
                   DateTime dt2 = Convert.ToDateTime(endTime);
                   query = from obj in query where obj.selltime <= dt2 orderby obj.id descending select obj;
               }
               if (!string.IsNullOrEmpty(startNum) && startNum != "null")
               {
                   float x = float.Parse(startNum);
                       query = from obj in query where obj.zhekou >= x orderby obj.id descending select obj;
               }
               if (!string.IsNullOrEmpty(endNum) && endNum != "null")
               {
                   float y = float.Parse(endNum);
                   query = from obj in query where obj.zhekou <= y orderby obj.id descending select obj;
               }
               return query;
          
           
       }

       public DataTable getbySelect()
       {
           ZG_OAEntities zg=new ZG_OAEntities();

           DateTime start = Convert.ToDateTime("2013/7/4 0:00:00");
           DateTime start2 = Convert.ToDateTime("2014/7/4 0:00:00");
           var m = from obj in zg.goodsInfo.Where(p => p.selltime > start && p.selltime < start2) 
                   join g in zg.goods on obj.gid equals g.goodsId
                  join gt in zg.goodsType on g.goodsTypeId equals SqlFunctions.StringConvert((double)gt.id)
                  ////  group obj by obj.gid into x
                   select new
                   {
                      
                       name=gt.typeName,
                       goodsprice = obj.goodsprice,
                       
                       num = obj.goodscount,
                        x=obj.zhekou
                   };
           DataTable dt = new DataTable();
           DataRow dr;
           DataColumn dc = new DataColumn("类型", typeof(int));
           DataColumn dc1 = new DataColumn("销售额", typeof(int));
           dt.Columns.Add(dc);
           dt.Columns.Add(dc1);
           foreach (var item in m)
           {
               dr = dt.NewRow();
               //dr["类型"] = item.name;
              // dr["销售额"] =Convert.ToDecimal(item.goodsprice) * Convert.ToDecimal( item.num) * Convert.ToDecimal(item.x)/10;
               dr["类型"]=item.x;
               dr["销售额"] = item.goodsprice;
               dt.Rows.Add(dr);
           }
           return dt;
       }
    }
}
